<?php

namespace App\Http\Controllers;

use App\Models\Config;
use Illuminate\Http\Request;

class ConfigController extends Controller
{
    public function index(Request $request)
    {
        $list = Config::where('user_id', $request->user()->id)->get();
        $data = [];
        foreach ($list as $v) {
            $data[$v['key']] = $v['value'];
        }
        return $this->response($data);
    }

    public function store(Request $request)
    {
        $obj = Config::where('key', $request->input('key'))->first();
        if (!is_object($obj)){
            $obj = new Config();
        }
        $obj->user_id = $request->user()->id;
        $obj->save();
        return $this->response($obj);
    }
}
